package tools;
import java.util.ArrayList;
import java.util.List;

import model.Braille;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/** 
 * 汉字转12点阵盲文算法
 * @Title: ChineseToBraille
 * @author John Lee 李正先
 * @Date: 2015-03-5
 * @注：只支持GB2312字符集中的汉字 
 */  
public class ChineseToBraille {

	public static void main(String[] args) {
		List<Braille> list =  toBraille("你好，北京欢迎您！");
		System.out.println();
		System.out.print("原来的拼音为：");
		for(Braille b : list){
			System.out.print(b.getSrc()+" ");
		}
	}

	
	/** 
     * 获得盲文的List列表
     * @param str 给定汉字串 
     * @return 盲文Braille对象的List列表
     */  
	public static List<Braille> toBraille(String src) {  
		List<Braille> brailleList=new ArrayList<Braille>();
        char[] t1 = null; 
        t1 = src.toCharArray();
        String[] t2 = new String[t1.length]; 
        // 设置汉字拼音输出的格式  
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat(); 
        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
        t3.setToneType(HanyuPinyinToneType.WITH_TONE_NUMBER);  
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);
        try {  
            for (int i =0; i < t1.length; i++) {  
                // 判断能否为汉字字符 
               if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) { 
            	   t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                   System.out.println(t2[0]);
                   Braille b = PinyinToBraille(t2[0]);
                   if(b!=null){
                       brailleList.add(b);
                   }
               } else { 
                   // 如果不是汉字字符
                    System.out.println(Character.toString(t1[i]));
                    Braille b = FuhaoToBraille(Character.toString(t1[i]));
                    if(b!=null){
                        brailleList.add(b);
                    }
                }  
            }  
       } catch (BadHanyuPinyinOutputFormatCombination e) { 
           e.printStackTrace();  
        }  
       return brailleList;
    }
	

	/** 
     * 符号转盲文
     * @param 符号
     * @return 盲文Braille对象
     */  
	private static Braille FuhaoToBraille(String fuhao) {
		Braille b = new Braille(fuhao);
		switch (fuhao) {
		case "，":
		case ",":
			b.setEIGHT(true);
			break;
		case "。":
		case ".":
			b.setSEVEN(true);
			b.setEIGHT(true);
			break;
		case "；":
		case ";":
			b.setEIGHT(true);
			b.setNINE(true);
			break;
		case "？":
		case "?":
			b.setONE(true);
			b.setEIGHT(true);
			break;
		case "！":
		case "!":
			b.setONE(true);
			b.setSEVEN(true);
			b.setEIGHT(true);
			break;
		case "：":
		case ":":
			b.setONE(true);
			b.setEIGHT(true);
			b.setNINE(true);
			break;
		case "、":
		case "\\":
		case "（":
		case "(":
		case "）":
		case ")":
		case "“":
		case "\"":
		case "”":
		case "……":
		case "^":
		case "…":
			b.setZERO(true);
			break;
		default:
			break;
		}
		return b;
	}
	

	/** 
     * 拼音转盲文
     * @param 拼音
     * @return 盲文Braille对象
     */  
	private static Braille PinyinToBraille(String pinyin) {
		boolean isQ = false;
		Braille b = new Braille(pinyin);
		System.out.println("声调是："+pinyin.charAt(pinyin.length()-1));
		switch (Integer.parseInt(pinyin.charAt(pinyin.length()-1)+"")) {
		case 1:
			b.setA(true);
			break;
		case 2:
			b.setB(true);
			break;
		case 3:
			b.setA(true);
			b.setB(true);
			break;
		default:
			break;
		}
		
		switch (pinyin.charAt(0)) {
		case 'b':
			b.setONE(true);
			b.setTHREE(true);
			break;
		case 'p':
			b.setTWO(true);
			b.setTHREE(true);
			break;
		case 'm':
			b.setONE(true);
			b.setTWO(true);
			b.setTHREE(true);
			break;
		case 'f':
			b.setTHREE(true);
			break;
		case 'd':
			b.setONE(true);
			b.setFOUR(true);
			break;
		case 't':
			b.setTWO(true);
			b.setFOUR(true);
			break;
		case 'n':
			b.setONE(true);
			b.setTWO(true);
			b.setFOUR(true);
			break;
		case 'l':
			b.setFOUR(true);
			break;
		case 'r':
			b.setTHREE(true);
			b.setFOUR(true);
			break;
		case 'z':
			b.setONE(true);
			b.setTHREE(true);
			b.setFOUR(true);
			if(pinyin.charAt(1)!='h'){
				b.setFIVE(true);
			}else{
				isQ=true;
			}
			break;
		case 'c':
			b.setTWO(true);
			b.setTHREE(true);
			b.setFOUR(true);
			if(pinyin.charAt(1)!='h'){
				b.setFIVE(true);
			}else{
				isQ=true;
			}
			break;
		case 's':
			b.setONE(true);
			b.setTWO(true);
			b.setTHREE(true);
			b.setFOUR(true);
			if(pinyin.charAt(1)!='h'){
				b.setFIVE(true);
			}else{
				isQ=true;
			}
			break;
		case 'g':
			b.setONE(true);
			break;
		case 'j':
			b.setONE(true);
			break;
		case 'k':
			b.setTWO(true);
			break;
		case 'q':
			b.setTWO(true);
			break;
		case 'h':
			b.setONE(true);
			b.setTWO(true);
			break;
		case 'x':
			b.setONE(true);
			b.setTWO(true);
			break;
		default:
			break;
		}
		String yunmu = null;
		if(isQ){
			yunmu = pinyin.substring(2,pinyin.length()-1);
		}else{
			yunmu = pinyin.substring(1,pinyin.length()-1);
		}
		System.out.println("韵母："+yunmu);
		switch (yunmu) {
		case "i":
			b.setFIVE(true);
			break;
		case "u":
			b.setSIX(true);
			break;
		case "a":
			b.setSEVEN(true);
			break;
		case "o":
			b.setSEVEN(true);
			b.setEIGHT(true);
			b.setNINE(true);
			b.setZERO(true);
			break;
		case "e":
			b.setEIGHT(true);
			b.setNINE(true);
			b.setZERO(true);
			break;
		case "ie":
			b.setEIGHT(true);
			b.setNINE(true);
			b.setZERO(true);
			break;
		case "ai":
			b.setSEVEN(true);
			b.setNINE(true);
			break;
		case "ei":
			b.setNINE(true);
			break;
		case "ao":
			b.setSEVEN(true);
			b.setEIGHT(true);
			b.setZERO(true);
			break;
		case "ou":
			b.setEIGHT(true);
			b.setZERO(true);
			break;
		case "an":
			b.setSEVEN(true);
			b.setZERO(true);
			break;
		case "en":
			b.setZERO(true);
			break;
		case "ang":
			b.setSEVEN(true);
			b.setNINE(true);
			b.setZERO(true);
			break;
		case "eng":
			b.setNINE(true);
			b.setZERO(true);
			break;
		case "er":
			b.setSEVEN(true);
			b.setEIGHT(true);
			b.setNINE(true);
			break;
		default:
			break;
		}
		return b;
	}
}
